package com.example.thirdhomework

import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.text.Editable
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.thirdhomework.databinding.ActivityFirstBinding

class FirstActivity : AppCompatActivity() {
    private val binding by lazy {
        ActivityFirstBinding.inflate(layoutInflater)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getStringExtra(KEY1)?.let {
                binding.textView1.text = Editable.Factory.getInstance().newEditable(it.toString())
            }
        }else{
            intent.getStringExtra(KEY1)?.let {
                binding.textView1.text = Editable.Factory.getInstance().newEditable(it.toString())
            }
        }

        val intent = Intent(this, FirstActivity::class.java)

        binding.backbutton.setOnClickListener {
            val resultIntent = Intent()
            resultIntent.putExtra(KEY1, binding.textView1.text.toString())
            setResult(RESULT_OK, resultIntent)
            finish()
        }
    }
}